{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# B 09-2 Yates' Correlation for $\\chi^2$ Continuity\n",
    "\n",
    "## 案例\n",
    "\n",
    "分析胞磷胆碱和神经节苷脂治疗脑血管疾病的有效性差异。\n",
    "\n",
    "## 分析\n",
    "\n",
    "影响变量：药物治疗 / 安慰剂 ———— 二项分类变量  \n",
    "结果变量：有效 / 无效 ———— 二项分类变量\n",
    "\n",
    "采用 $ 2 \\times 2 $ 四格表 $\\chi^2$ 检验推断两个整体的有效率是否相同。\n",
    "\n",
    "## 计算\n",
    "\n",
    "数据如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (2, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>group</th><th>effective_number</th><th>uneffective_number</th></tr><tr><td>str</td><td>i64</td><td>i64</td></tr></thead><tbody><tr><td>&quot;medicine1&quot;</td><td>25</td><td>3</td></tr><tr><td>&quot;medicine2&quot;</td><td>24</td><td>6</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (2, 3)\n",
       "┌───────────┬──────────────────┬────────────────────┐\n",
       "│ group     ┆ effective_number ┆ uneffective_number │\n",
       "│ ---       ┆ ---              ┆ ---                │\n",
       "│ str       ┆ i64              ┆ i64                │\n",
       "╞═══════════╪══════════════════╪════════════════════╡\n",
       "│ medicine1 ┆ 25               ┆ 3                  │\n",
       "│ medicine2 ┆ 24               ┆ 6                  │\n",
       "└───────────┴──────────────────┴────────────────────┘"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import polars as pl\n",
    "\n",
    "df = pl.read_csv(\"B_09_2-data.csv\")\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "理论频数矩阵如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (2, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>group</th><th>theoretical_effective_frequency</th><th>theoretical_uneffective_frequency</th></tr><tr><td>str</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>&quot;medicine1&quot;</td><td>23.655172</td><td>4.344828</td></tr><tr><td>&quot;medicine2&quot;</td><td>25.344828</td><td>4.655172</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (2, 3)\n",
       "┌───────────┬─────────────────────────────────┬─────────────────────────────────┐\n",
       "│ group     ┆ theoretical_effective_frequenc… ┆ theoretical_uneffective_freque… │\n",
       "│ ---       ┆ ---                             ┆ ---                             │\n",
       "│ str       ┆ f64                             ┆ f64                             │\n",
       "╞═══════════╪═════════════════════════════════╪═════════════════════════════════╡\n",
       "│ medicine1 ┆ 23.655172                       ┆ 4.344828                        │\n",
       "│ medicine2 ┆ 25.344828                       ┆ 4.655172                        │\n",
       "└───────────┴─────────────────────────────────┴─────────────────────────────────┘"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "def calc_theoretical_frequency(array: np.ndarray):\n",
    "    # 计算行和、列和以及总和\n",
    "    row_sums = array.sum(axis=1, keepdims=True)  # 保持维度以进行广播\n",
    "    col_sums = array.sum(axis=0)\n",
    "    all_sum = array.sum()\n",
    "\n",
    "    # 验证总和的一致性 (可选)\n",
    "    if not np.isclose(row_sums.sum(), all_sum) or not np.isclose(col_sums.sum(), all_sum):\n",
    "        raise ValueError(\"The sum of row sums or column sums does not match the total sum.\")\n",
    "\n",
    "    # 计算理论频数\n",
    "    res = (row_sums * col_sums) / all_sum\n",
    "    \n",
    "    return res\n",
    "\n",
    "input_tb = df.select(\"effective_number\", \"uneffective_number\").to_numpy()\n",
    "tf = calc_theoretical_frequency(input_tb)\n",
    "\n",
    "df_T = df.select(\"group\").with_columns(pl.DataFrame(tf, schema=[\"theoretical_effective_frequency\", \"theoretical_uneffective_frequency\"]))\n",
    "df_T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--将此行转换为 LaTeX 语法：\n",
    "可见，存在T属于T_{ij}, i,j属于 (1,2), 1<=T_{ij}<=5-->\n",
    "可见，$ \\forall\\ T_{ij},\\ i,j \\in (1,2),\\ 1\\leq T_{ij}\\leq 5 $. 因而需要使用四格表资料 $ \\chi^2 $ 检验的校正公式修正 P 值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chi-squared value = 0.3759259259259261\n",
      "p-value = 0.5397917174425446\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import chi2_contingency\n",
    "\n",
    "result = chi2_contingency(df.select(\"effective_number\", \"uneffective_number\"), correction=True)\n",
    "\n",
    "print(f\"chi-squared value = {result.statistic}\")\n",
    "print(f\"p-value = {result.pvalue}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "P > 0.05, 不能拒绝 $ H_{0} $，不能认为两种药物在治疗脑血管疾病的有效率不等。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
